<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>find_photos (TripsController)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File trips_controller.rb, line 167</span>
        <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">find_photos</span>
                <span class="ruby-ivar">@title</span> = <span class="ruby-value str">'Fotos del Itinerario'</span>
                <span class="ruby-ivar">@trip</span> = <span class="ruby-constant">Trip</span>.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:trip_id</span>])
                <span class="ruby-ivar">@iti_location</span> = <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:iti_location</span>] 
                <span class="ruby-identifier">tags</span> = <span class="ruby-ivar">@trip</span>.<span class="ruby-identifier">tri_country</span> <span class="ruby-operator">+</span> <span class="ruby-value str">' '</span> <span class="ruby-operator">+</span> <span class="ruby-ivar">@trip</span>.<span class="ruby-identifier">tri_city</span> <span class="ruby-operator">+</span> <span class="ruby-value str">' '</span> <span class="ruby-operator">+</span> <span class="ruby-ivar">@iti_location</span>
                <span class="ruby-ivar">@latitude</span> = <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:latitude</span>]
                <span class="ruby-ivar">@longitude</span> = <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:longitude</span>]
                <span class="ruby-comment cmt"># creamos la lista de parametros para agregar al URL</span>
                <span class="ruby-identifier">hash</span> = {<span class="ruby-identifier">:text</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">tags</span>, <span class="ruby-identifier">:per_page</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">27</span>, <span class="ruby-identifier">:tag_mode</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'all'</span>}

                <span class="ruby-comment cmt"># creamos un query string uniedo clave y valor del hash anterior y los separamos por &amp;</span>
                <span class="ruby-identifier">parameters</span> = <span class="ruby-constant">URI</span>.<span class="ruby-identifier">escape</span>(<span class="ruby-identifier">hash</span>.<span class="ruby-identifier">to_a</span>.<span class="ruby-identifier">collect</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">pair</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pair</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">'='</span>)}.<span class="ruby-identifier">join</span>(<span class="ruby-value str">'&amp;'</span>))

                <span class="ruby-identifier">results</span> = <span class="ruby-value str">''</span>
                <span class="ruby-ivar">@pics</span> = []
                
                <span class="ruby-identifier">open</span>(<span class="ruby-constant">FLICKR_SEARCH_URL</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'&amp;'</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">parameters</span>) {
                  <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span> <span class="ruby-identifier">results</span> = <span class="ruby-constant">XmlSimple</span><span class="ruby-operator">::</span><span class="ruby-identifier">xml_in</span>(<span class="ruby-identifier">s</span>.<span class="ruby-identifier">read</span>, <span class="ruby-value str">'force_array'</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">false</span>)
                }

                <span class="ruby-comment cmt"># EL XML con el resultado contiene un tag llamado &quot;photos&quot; y dentro</span>
                <span class="ruby-comment cmt"># del existen muchos tags de nombre &quot;photo&quot; para cada uno de ellos</span>
                <span class="ruby-comment cmt"># obtenemos URL y titulo de la foto</span>
                
                <span class="ruby-identifier">results</span>[<span class="ruby-value str">'photos'</span>][<span class="ruby-value str">'photo'</span>].<span class="ruby-identifier">each</span>{        
                        <span class="ruby-operator">|</span><span class="ruby-identifier">p</span><span class="ruby-operator">|</span>  <span class="ruby-ivar">@pics</span> <span class="ruby-operator">&lt;&lt;</span> [<span class="ruby-node">&quot;http://farm#{p['farm']}.static.flickr.com/#{p['server']}/#{p['id']}_#{p['secret']}.jpg&quot;</span>]
                }
                <span class="ruby-identifier">flash</span>[<span class="ruby-identifier">:notice</span>] = <span class="ruby-value str">''</span>
                <span class="ruby-keyword kw">rescue</span>
                        <span class="ruby-identifier">flash</span>[<span class="ruby-identifier">:notice</span>] = <span class="ruby-value str">'No se consiguieron fotos para el destino. Intenta de nuevo!'</span>
                        <span class="ruby-identifier">render</span> <span class="ruby-identifier">:template</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'trips/flickr'</span>
                        <span class="ruby-comment cmt">#redirect_to :controller =&gt; 'trips', :action =&gt; 'flickr'</span>
                
    <span class="ruby-keyword kw">end</span></pre>
</body>
</html>